<?php include "../public/header.php"?>
<body class="dialogPage">
<div id="dialog">
    <div class="page-title" id="page-title">
        <div class="page-title-con">
            <span class="title">选择用户(单选)</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button class="layui-btn layui-btn-sm btn-dark marginr5" type="button" id="submit">确定</button>
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>

    <div class="layui-fluid">
        <div class="layui-row">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body search-div">
                        <div class="layui-form">
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" id="keyword" style="width: 300px;float: left;" autocomplete="off" placeholder="姓名" class="layui-input">
                                <button class="layui-btn layui-btn-sm btn-light" type="button" id="search" style="margin-left: 5px;"><i class="layui-icon layui-icon-search"></i>查询</button>
                                <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="location.reload()" ><i class="layui-icon layui-icon-refresh"></i>重置</button>
                            </div>
                        </div>
                    </div>
                    <div class="layui-card-body">
                        <div id="tree_content" class="demo-tree demo-tree-box"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    // 重选设置默认选中
    var oldData = getQueryVariable('old_data');
    var oldDataIdArr = [];
    if(oldData){
        oldDataIdArr = oldData.split(',');
    }

    // 页面绑定节点元素
    var parent_dom = getQueryVariable('dom');

    var data = [];
    layui.use(['table','form','element','tree','facade'],function() {
        var table = layui.table;
        var tree = layui.tree;
        var facade = layui.facade;

        facade.ajax({url: ask_opt_get_dept_user}).done(function(res){
            if (res.code === 2000) {
                //常规用法
                tree.render({
                    elem: '#tree_content' //默认是点击节点可进行收缩
                    ,onlyIconControl: true
                    ,data: res.data
                    ,click: function (obj) {
                        if(obj.data.children == undefined){
                            data = obj.data
                            $('.layui-tree-entry').each(function () {
                                $(this).removeClass('layui-tree-other');
                            });
                            $(obj.elem).find('.layui-tree-entry').eq(0).addClass('layui-tree-other');
                        }
                    }
                });

                // 处理原有的图标
                $('.layui-tree-entry').each(function () {
                    if($(this).next().length === 0){
                        $(this).find('.layui-tree-iconClick').remove();
                    }
                });
                $('.layui-tree-txt').css('text-decoration','none');
            }
        });

        // 查询数据-以前的查询条件都会保留
        $('#search').on('click', function(){
            var keyword = $('#keyword').val();

            facade.ajax({url: ask_opt_get_dept_user,data:{keyword:keyword}}).done(function(res){
                if (res.code === 2000) {
                    //常规用法
                    tree.render({
                        elem: '#test1' //默认是点击节点可进行收缩
                        ,onlyIconControl: true
                        ,data: res.data
                        ,click: function (obj) {
                            if(obj.data.children == undefined){
                                data = obj.data

                                $('.layui-tree-entry').each(function () {
                                    $(this).find('.layui-tree-txt').removeClass('layui-tree-other');
                                });
                                $(obj.elem).find('.layui-tree-entry').eq(0).find('.layui-tree-txt').addClass('layui-tree-other');
                            }
                        }
                    });

                    $('.layui-tree-entry').each(function () {
                        if($(this).next().length === 0){
                            $(this).find('.layui-tree-iconClick').remove();
                        }
                    });
                    $('.layui-tree-txt').css('text-decoration','none');
                }
            });
        });

        // 确定
        $('#submit').on('click', function () {
            if(data.length === 0){
                facade.error('请选择人员'); return false;
            }
            var str = '<button type="button" class="layui-btn layui-btn-sm btn-dark" key="'+data.id+'"><span class="choose_content">'+data.name+'' +
                '</span><span class="choose_close" onclick="choose_close(this)">×</span></button>';
            $("#"+parent_dom , parent.document).html(str);
            layer_close();
        });
    });
</script>
</body>
</html>
